﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using CoNatural.Data.VisualStudio.AddIn.Helpers;
using CoNatural.Data.VisualStudio.AddIn.Controls;

namespace CoNatural.Data.VisualStudio.AddIn.Forms {
	public partial class TablesTemplateWizardForm : SelectedItemWizardForm {
		private string connectionString;
		private TemplateBuilder templateBuilder;

		private SelectConnectionControl step0;
		private SelectTablesControl step1;
		private SelectNamingConventionsControl step2;
		private SelectTemplateControl step3;
		private IList<Model.DbObject> selection;

		public TablesTemplateWizardForm(SelectedItem selectedItem)
			: base(selectedItem) {
			InitializeComponent();

			this.Text = "CoNatural Tables Template Import Wizard - " + RootNamespace;

			step0 = new SelectConnectionControl();
			step1 = new SelectTablesControl();
			step2 = new SelectNamingConventionsControl();
			step3 = new SelectTemplateControl(ProjectPath);

			Initialize(
				new WizardStep(step0, false, false, "Step 1 of 4 - Select the data source.", new[] { "1) Select the connection string to the data source." }),
				new WizardStep(step1, false, true, "Step 2 of 4 - Select the tables.", new[] { "1) Select the tables you want to import as CoNatural data commands (CRUD).", "2) You can filter by schema and/or name prefix.", "3) Use the Refresh button to validate your selection." }),
				new WizardStep(step2, false, true, "Step 3 of 4 - Define the naming conventions.", new[] { "1) Define the naming conventions you want to apply to your commands.", "2) Use a separator character to break command names into namespace parts, and enter the part numbers you want to skip (separated by spaces).", "3) Use the Refresh button to validate your rules." }),
				new WizardStep(step3, true, true, "Step 4 of 4 - Select table template.", new[] { "Select the table template you want to use." })
			);
		}

		protected override bool OnGoBack() {
			return base.OnGoBack();
		}

		protected override bool OnGoNext() {
			if (CurrentStepIndex == 0) {
				connectionString = step0.GetSelectedConnectionString();
				if (!string.IsNullOrEmpty(connectionString)) {
					step1.Initialize(connectionString);
					return true;
				}
			}
			else if (CurrentStepIndex == 1) {
				selection = step1.GetSelection();
				if (selection.Count > 0) {
					step2.Initialize(selection, false);
					return true;
				}
			}
			else if (CurrentStepIndex == 2) {
				step3.Initialize(RootNamespace, selection);
				return true;
			}

			return false;
		}

		protected override bool OnFinish(BackgroundWorker worker) {
			// import selected tables
			var server = new Server(new ServerConnection(new System.Data.SqlClient.SqlConnection(connectionString)));
			var db = server.Databases[server.ConnectionContext.DatabaseName];
			templateBuilder = new Helpers.TemplateBuilder(RootCollection, RootPath, RootNamespace);
			int row = 0;
			double total = (double)selection.Count;
			var templates = step3.SelectedTemplates;
			foreach (Model.DbObject tb in selection) {
				if (worker.CancellationPending)
					return false;
				foreach(var templatePath in templates)
					templateBuilder.Create(tb, db.Tables[tb.Name, tb.Schema], templatePath);
				worker.ReportProgress((int)(((double)++row / total) * 100.0));
			}
			return true;
		}
	}
}
